class LookupTable:
    """A simple class to lookup 2 licenses and give the resultant license"""

    def __init__(self):
        """Construct the default license lookup table using a dict:
        If an 'X' is given for the value that means the 2 licenses cannot be combined.
        If a string other than an 'X' is given that means the license corresponding to that string will be the resultant license.

        The following code is used for convenience:
        NL = No License
        PD = Public Domain (http://creativecommons.org/publicdomain/)
        CC0 = CC-Zero (http://creativecommons.org/license/zero)
        BY = Attribution (http://creativecommons.org/licenses/by/3.0/)
        NC = Non-Commercial
        ND = No Derivatives
        SA = Share-Alke

        this lookup table is based on the diagram "license_lookup.jpg"
        """
        self.table = {("PD","PD"): "PD",
                      ("PD","CC0"): "PD",
                      ("PD","BY"): "X",
                      ("PD","BY-NC"): "X",
                      ("PD","BY-NC-ND"): "X",
                      ("PD","BY-NC-ND-SA"): "X",
                      ("PD","BY-NC-SA"): "X",
                      ("PD","BY-ND"): "X",
                      ("PD","BY-ND-SA"): "X",
                      ("PD","BY-SA"): "X",
                      ("PD","SA"): "X",
                      ("PD","ARR"): "X",
                      ("PD","NL"): "X",
                      ("CC0","PD"): "CC0",
                      ("CC0","CC0"): "CC0",
                      ("CC0","BY"): "X",
                      ("CC0","BY-NC"): "X",
                      ("CC0","BY-NC-ND"): "X",
                      ("CC0","BY-NC-ND-SA"): "X",
                      ("CC0","BY-NC-SA"): "X",
                      ("CC0","BY-ND"): "X",
                      ("CC0","BY-ND-SA"): "X",
                      ("CC0","BY-SA"): "X",
                      ("CC0","ARR"): "X",
                      ("CC0","NL"): "X",
                      ("BY","PD"): "BY",
                      ("BY","CC0"): "BY",
                      ("BY","BY"): "BY",
                      ("BY","BY-NC"): "X",
                      ("BY","BY-NC-ND"): "X",
                      ("BY","BY-NC-ND-SA"): "X",
                      ("BY","BY-NC-SA"): "X",
                      ("BY","BY-ND"): "X",
                      ("BY","BY-ND-SA"): "X",
                      ("BY","BY-SA"): "X",
                      ("BY","ARR"): "X",
                      ("BY","NL"): "X",
                      ("BY-NC","PD"): "BY-NC",
                      ("BY-NC","CC0"): "BY-NC",
                      ("BY-NC","BY"): "BY-NC",
                      ("BY-NC","BY-NC"): "BY-NC",
                      ("BY-NC","BY-NC-ND"): "X",
                      ("BY-NC","BY-NC-ND-SA"): "X",
                      ("BY-NC","BY-NC-SA"): "X",
                      ("BY-NC","BY-ND"): "X",
                      ("BY-NC","BY-ND-SA"): "X",
                      ("BY-NC","BY-SA"): "X",
                      ("BY-NC","ARR"): "X",
                      ("BY-NC","NL"): "X",
                      ("BY-NC-ND","PD"): "BY-NC-ND",
                      ("BY-NC-ND","CC0"): "BY-NC-ND",
                      ("BY-NC-ND","BY"): "BY-NC-ND",
                      ("BY-NC-ND","BY-NC"): "BY-NC-ND",
                      ("BY-NC-ND","BY-NC-ND"): "BY-NC-ND",
                      ("BY-NC-ND","BY-NC-ND-SA"): "X",
                      ("BY-NC-ND","BY-NC-SA"): "X",
                      ("BY-NC-ND","BY-ND"): "BY-NC-ND",
                      ("BY-NC-ND","BY-ND-SA"): "X",
                      ("BY-NC-ND","BY-SA"): "X",
                      ("BY-NC-ND","ARR"): "X",
                      ("BY-NC-ND","NL"): "X",
                      ("BY-NC-ND-SA","PD"): "BY-NC-ND-SA",
                      ("BY-NC-ND-SA","CC0"): "BY-NC-ND-SA",
                      ("BY-NC-ND-SA","BY"): "BY-NC-ND-SA",
                      ("BY-NC-ND-SA","BY-NC"): "BY-NC-ND-SA",
                      ("BY-NC-ND-SA","BY-NC-ND"): "BY-NC-ND-SA",
                      ("BY-NC-ND-SA","BY-NC-ND-SA"): "BY-NC-ND-SA",
                      ("BY-NC-ND-SA","BY-NC-SA"): "X",
                      ("BY-NC-ND-SA","BY-ND"): "BY-NC-ND-SA",
                      ("BY-NC-ND-SA","BY-ND-SA"): "X",
                      ("BY-NC-ND-SA","BY-SA"): "X",
                      ("BY-NC-ND-SA","SA"): "X",
                      ("BY-NC-ND-SA","ARR"): "X",
                      ("BY-NC-ND-SA","NL"): "X",
                      ("BY-NC-SA","PD"): "BY-NC-SA",
                      ("BY-NC-SA","CC0"): "BY-NC-SA",
                      ("BY-NC-SA","BY"): "BY-NC-SA",
                      ("BY-NC-SA","BY-NC"): "BY-NC-SA",
                      ("BY-NC-SA","BY-NC-ND"): "X",
                      ("BY-NC-SA","BY-NC-ND-SA"): "X",
                      ("BY-NC-SA","BY-NC-SA"): "BY-NC-SA",
                      ("BY-NC-SA","BY-ND"): "X",
                      ("BY-NC-SA","BY-ND-SA"): "X",
                      ("BY-NC-SA","BY-SA"): "X",
                      ("BY-NC-SA","SA"): "X",
                      ("BY-NC-SA","ARR"): "X",
                      ("BY-NC-SA","NL"): "X",
                      ("BY-ND","PD"): "BY-ND",
                      ("BY-ND","CC0"): "BY-ND",
                      ("BY-ND","BY"): "BY-ND",
                      ("BY-ND","BY-NC"): "X",
                      ("BY-ND","BY-NC-ND"): "X",
                      ("BY-ND","BY-NC-ND-SA"): "X",
                      ("BY-ND","BY-NC-SA"): "X",
                      ("BY-ND","BY-ND"): "BY-ND",
                      ("BY-ND","BY-ND-SA"): "X",
                      ("BY-ND","BY-SA"): "X",
                      ("BY-ND","SA"): "X",
                      ("BY-ND","ARR"): "X",
                      ("BY-ND","NL"): "X",
                      ("BY-ND-SA","PD"): "BY-ND-SA",
                      ("BY-ND-SA","CC0"): "BY-ND-SA",
                      ("BY-ND-SA","BY"): "BY-ND-SA",
                      ("BY-ND-SA","BY-NC"): "X",
                      ("BY-ND-SA","BY-NC-ND"): "X",
                      ("BY-ND-SA","BY-NC-ND-SA"): "X",
                      ("BY-ND-SA","BY-NC-SA"): "X",
                      ("BY-ND-SA","BY-ND"): "BY-ND-SA",
                      ("BY-ND-SA","BY-ND-SA"): "BY-ND-SA",
                      ("BY-ND-SA","BY-SA"): "X",
                      ("BY-ND-SA","SA"): "X",
                      ("BY-ND-SA","ARR"): "X",
                      ("BY-ND-SA","NL"): "X",
                      ("BY-SA","PD"): "BY-SA",
                      ("BY-SA","CC0"): "BY-SA",
                      ("BY-SA","BY"): "X",
                      ("BY-SA","BY-NC"): "X",
                      ("BY-SA","BY-NC-ND"): "X",
                      ("BY-SA","BY-NC-ND-SA"): "X",
                      ("BY-SA","BY-NC-SA"): "X",
                      ("BY-SA","BY-ND"): "X",
                      ("BY-SA","BY-ND-SA"): "X",
                      ("BY-SA","BY-SA"): "BY-SA",
                      ("BY-SA","SA"): "X",
                      ("BY-SA","ARR"): "X",
                      ("BY-SA","NL"): "X",
                      ("ARR","PD"): "ARR",
                      ("ARR","CC0"): "ARR",
                      ("ARR","BY"): "ARR",
                      ("ARR","BY-NC"): "X",
                      ("ARR","BY-NC-ND"): "X",
                      ("ARR","BY-NC-ND-SA"): "X",
                      ("ARR","BY-NC-SA"): "X",
                      ("ARR","BY-ND"): "X",
                      ("ARR","BY-ND-SA"): "X",
                      ("ARR","BY-SA"): "X",
                      ("ARR","SA"): "X",
                      ("ARR","ARR"): "X",
                      ("ARR","NL"): "X",
                      ("NL","PD"): "NL",
                      ("NL","CC0"): "NL",
                      ("NL","BY"): "NL",
                      ("NL","BY-NC"): "X",
                      ("NL","BY-NC-ND"): "X",
                      ("NL","BY-NC-ND-SA"): "X",
                      ("NL","BY-NC-SA"): "X",
                      ("NL","BY-ND"): "X",
                      ("NL","BY-ND-SA"): "X",
                      ("NL","BY-SA"): "X",
                      ("NL","SA"): "X",
                      ("NL","ARR"): "X",
                      ("NL","NL"): "X",
                      ("PD","X"): "X",
                      ("CC0","X"): "X",
                      ("BY","X"): "X",
                      ("BY-NC","X"): "X",
                      ("BY-NC-ND","X"): "X",
                      ("BY-NC-ND-SA","X"): "X",
                      ("BY-NC-SA","X"): "X",
                      ("BY-ND","X"): "X",
                      ("BY-ND-SA","X"): "X",
                      ("BY-SA","X"): "X",
                      ("ARR","X"): "X",
                      ("NL","X"): "X",
                      ("X","X"): "X"}

    def query(self, license, licenselist):
        """Recursive function to lookup the resultant license given the license components
        The 'licenselist' should always be sent as a list, and the 'license' should always be sent as a string"""
        if len(licenselist) == 1:
            return self.table[(license, licenselist[0])]
        else:
            return self.table[(license, self.query(licenselist[0], licenselist[1:len(licenselist)]))]

    


